package com.secsys.action.price;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.secsys.action.BaseAction;
import com.secsys.dto.AssistDto;
import com.secsys.dto.StoStockDTO;
import com.secsys.dto.StoStockdailyDTO;
import com.secsys.model.TbStoStockdaily;
import com.secsys.service.StoStockService;
import com.secsys.service.StoStockdailyService;
import com.secsys.util.DateFormatUtils;
/**
 * <pre>
 * 	处理股价分析中的“高价排行榜”、“低价排行榜”，
 * 	prepareTopPrice、searchTopPrice、topPriceDetail、doTopPriceSearch分别为默认处理、用户搜索、详细信息、内部协助函数。
 *@作者 tiger.hu.tiger@gmail.com
 *@创建日期 2011-5-8
 *@版本1.0
 */
public class TopPriceAction extends BaseAction<TbStoStockdaily> {
	
	private static final long serialVersionUID = -4848035276162751224L;

	/**
	 * 规定分页大小为10
	 */
	private final int limit=10; 
	
	private TbStoStockdaily tbStoStockdaily;
	private List<StoStockdailyDTO> stoStockdailyDTOList;
	private List<StoStockdailyDTO> oldYearStoStockdailyDTOList;
	private List<Date> operateDateList;
	private List<Double> startPriceList;
	private List<Double> endPriceList;
	
	private List<StoStockDTO> stockDTOList;
	private AssistDto assistDto; 
	private StoStockDTO stockDTO;
	private StoStockService stoStockService;
	private StoStockdailyService stoStockdailyService;
	
	
	/**
	 * 股价分析中"高价排行榜、低价排行榜",默认处理、初始化处理
	 * @return
	 * @throws Exception
	 */
	public String prepareTopPrice()throws Exception{
		if(assistDto==null){
			assistDto=new AssistDto();
			/**
			 * 默认情况显示高价排行榜
			 */
			assistDto.setUpdown(1);
		}
		assistDto.setStartDate(DateFormatUtils.convertStringToDate("1997-6-3"));
		assistDto.setCurrentPages(0);
		assistDto.setTotalPages(0);
		
		this.doTopPriceSearch();
		
		return "search";
	}
	
	/**
	 * 股价分析中"高价、低价排行榜"（按照时间、升降幅度），用户搜索
	 * @return
	 * @throws Exception
	 */
	public String searchTopPrice()throws Exception{
		if (assistDto.getStartDate()==null || assistDto.getStartDate().equals("")) {
			System.out.println("null start Date.");
			assistDto.setStartDate(DateFormatUtils.convertStringToDate("1998-6-3") );
		}
		if (!this.doTopPriceSearch()) {
			System.out.println("======搜索失败==============");
			return "prepare";
		}
		return "search";
	}

	
	/**
	 * 股价分析中"高价、低价排行榜"（按照时间、升降幅度）,内部协作
	 * @return
	 * @throws Exception
	 */
	public boolean doTopPriceSearch(){
		List<StoStockdailyDTO> tempStartList=this.stoStockdailyService.queryStoStockdailyByDate(assistDto.getStartDate(),0,0,2);
		Date startDate=null;
		/**
		 * 查询最近日期的一条数据，设置开始、结束时间
		 */
		System.out.println(tempStartList.size()+"-tempStartList--size");
		if (null!=tempStartList && tempStartList.size()>=1) {
			StoStockdailyDTO tempStart=tempStartList.get(0);
			startDate=DateFormatUtils.convertStringToDate(DateFormatUtils.convertDateToString(tempStart.getOperateDate())); 
		}else {
			return false;
		}
		/**
		 * 1为高价排行榜、0为低价排行榜
		 */
		int updown=assistDto.getUpdown();
				/**
		 * 设置当前页
		 */
		if (0 == assistDto.getCurrentPages()) {
			assistDto.setCurrentPages(1);
		}
		/**
		 * 偏移值= (当前第几页-1)*每页个数
		 */
		int offset = (assistDto.getCurrentPages() - 1) * limit;
		/**
		 * 查询得到的总记录数,也就是个股的数量
		 */
		int num=stoStockService.count();
		/**
		 * 总共多少页
		 */
		int totalPages = (num / limit);
		if (num % limit > 0) {
			totalPages++;
		}
		
		if (assistDto.getCurrentPages() >totalPages) {
			assistDto.setCurrentPages(0); 
		}

		assistDto.setTotalPages(totalPages);
//		startDate=DateFormatUtils.convertStringToDate("2010-12-1"); 
		HashMap<String,List> map=stoStockdailyService.queryTopPrice(startDate, updown, offset, limit); 
		this.stoStockdailyDTOList=map.get("stoStockdailyDTOList");
		for (StoStockdailyDTO stoStockdailyDTO : tempStartList) {
			System.out.println(stoStockdailyDTO.getId()+" "+stoStockdailyDTO.getOperateDate()); 
		}
		this.stockDTOList=map.get("stockDTOList");
		this.operateDateList=map.get("operateDateList");
		this.startPriceList=map.get("startPriceList");
		this.endPriceList=map.get("endPriceList");
		
//		for (StoStockDTO stockDTO : stockDTOList) {
//			 System.out.println(stockDTO.getId()+"=="+stockDTO.getStockCode()+"=="+stockDTO.getStockName()+"=="+stockDTO.getStockBoard());
//		} 
		return true;
	}
	
	
	
	/**
	 * 股价分析中"高价、低价排行榜"（按照时间、升降幅度）,详细信息显示
	 * @return
	 * @throws Exception
	 */
	public String topPriceDetail()throws Exception{
//		stockDTO=stoStockService.findByStockCode(stockDTO.getStockCode());
//		if (0 == assistDto.getCurrentPages()) {
//			assistDto.setCurrentPages(1);
//		}
//		int offset = (assistDto.getCurrentPages() - 1) * 20;
//		int num=stoStockdailyService.countByStockCode(stockDTO.getStockCode()); 
//		int totalPages = (num / 20);
//		if (num % 20 > 0) {
//			totalPages++;
//		}
//		
//		if (assistDto.getCurrentPages() >totalPages) {
//			assistDto.setCurrentPages(1); 
//		}
//		System.out.println(totalPages+"总页数");
//		assistDto.setTotalPages(totalPages);
//		this.stoStockdailyDTOList=stoStockdailyService.queryStoStockdailyByStockCode(stockDTO.getStockCode(),offset,20);
		return "stockDailyDetail";
	}
	
	
	
	
	
	
	
	
	
	@Override
	public TbStoStockdaily getModel() {
		if (this.tbStoStockdaily==null) {
			tbStoStockdaily=new TbStoStockdaily();
		}
		return this.tbStoStockdaily;
	}
	public TopPriceAction(){
		if (assistDto==null) {
			this.assistDto=new AssistDto();
		}
		if (assistDto==null) {
			this.stockDTO=new StoStockDTO();
		}
		if (stockDTOList==null) {
			this.stockDTOList=new ArrayList<StoStockDTO>();
		}
		if (tbStoStockdaily==null) {
			this.tbStoStockdaily=new TbStoStockdaily();
		}
	}
	
	public TbStoStockdaily getTbStoStockdaily() {
		return tbStoStockdaily;
	}
	public void setTbStoStockdaily(TbStoStockdaily tbStoStockdaily) {
		this.tbStoStockdaily = tbStoStockdaily;
	}
	public List<StoStockdailyDTO> getStoStockdailyDTOList() {
		return stoStockdailyDTOList;
	}
	public void setStoStockdailyDTOList(List<StoStockdailyDTO> stoStockdailyDTOList) {
		this.stoStockdailyDTOList = stoStockdailyDTOList;
	}
	public List<StoStockdailyDTO> getOldYearStoStockdailyDTOList() {
		return oldYearStoStockdailyDTOList;
	}
	public void setOldYearStoStockdailyDTOList(
			List<StoStockdailyDTO> oldYearStoStockdailyDTOList) {
		this.oldYearStoStockdailyDTOList = oldYearStoStockdailyDTOList;
	}
	public List<StoStockDTO> getStockDTOList() {
		return stockDTOList;
	}
	public void setStockDTOList(List<StoStockDTO> stockDTOList) {
		this.stockDTOList = stockDTOList;
	}
	public AssistDto getAssistDto() {
		return assistDto;
	}
	public void setAssistDto(AssistDto assistDto) {
		this.assistDto = assistDto;
	}
	public StoStockDTO getStockDTO() {
		return stockDTO;
	}
	public void setStockDTO(StoStockDTO stockDTO) {
		this.stockDTO = stockDTO;
	}
	public StoStockService getStoStockService() {
		return stoStockService;
	}
	public void setStoStockService(StoStockService stoStockService) {
		this.stoStockService = stoStockService;
	}
	public StoStockdailyService getStoStockdailyService() {
		return stoStockdailyService;
	}
	public void setStoStockdailyService(StoStockdailyService stoStockdailyService) {
		this.stoStockdailyService = stoStockdailyService;
	}

	public List<Date> getOperateDateList() {
		return operateDateList;
	}

	public void setOperateDateList(List<Date> operateDateList) {
		this.operateDateList = operateDateList;
	}

	public List<Double> getStartPriceList() {
		return startPriceList;
	}

	public void setStartPriceList(List<Double> startPriceList) {
		this.startPriceList = startPriceList;
	}

	public List<Double> getEndPriceList() {
		return endPriceList;
	}

	public void setEndPriceList(List<Double> endPriceList) {
		this.endPriceList = endPriceList;
	} 
	
}
